﻿using System.Xml.Linq;
using IDeal.Extensions;
using IDeal.Models;
using IDeal.Services;

namespace IDeal.Serialization
{
    internal class DirectoryRequestXmlWriter : XmlMessageWriter<DirectoryRequest>
    {
        public DirectoryRequestXmlWriter(IIDealServices iDealServices) : base(iDealServices)
        {
        }

        protected override string RootElementName
        {
            get { return "DirectoryReq"; }
        }

        protected override string GetHashSource(DirectoryRequest instance)
        {
            return string.Format("{0}{1:d9}{2}", instance.CreatedDateTimeStamp.ToIDealFormat(), instance.MerchantId, instance.SubId);
        }

        protected override void SetupRootElement(DirectoryRequest instance, XElement rootElement)
        {
            var signature = CreateSignature(instance);

            rootElement.Add(new XElement
            (
                Ns + "Merchant",
                new XElement(Ns + "merchantID", instance.MerchantId),
                new XElement(Ns + "subID", instance.SubId),
                new XElement(Ns + "authentication", "SHA1_RSA"),
                new XElement(Ns + "token", signature.Fingerprint),
                new XElement(Ns + "tokenCode", signature.Token)
            ));
        }
    }
}
